<?php

    include_once 'DAO/TripDAO.class.php';
    include_once 'DAO/CityDAO.class.php';
    include_once 'DAO/CountryDAO.class.php';
    include_once 'Classes/Trip.class.php';
    session_start();

    // An o xrhsths exei kanei login elegxoume ta dedomena kai ta eisagoume
    // sthn bash h epistrefoume oti yparxei lathos
    if(isset($_SESSION['login'])){
        if($_SESSION['login']){
            $city = trim($_POST['city']);
            $country = trim($_POST['country']);
            $tocity = trim($_POST['tocity']);
            $tocountry = trim($_POST['tocountry']);
            $description = trim($_POST['description']);
            $fromdate = trim($_POST['fromdate']);
            $todate = trim($_POST['todate']);
            if(strlen($city) == 0 ||
               strlen($country) == 0 ||
               strlen($tocity) == 0 ||
               strlen($tocountry) == 0 ||
               strlen($description) == 0 ||
               strlen($fromdate) == 0 ||
               strlen($todate) == 0)
            {
                header('Location: index.php?tripErr=1');
            }
            else{
                $userId = $_SESSION['userId'];
                $cityDao = new CityDAO();
                $countryDao = new CountryDAO();
                $countryDao->insertCountry($country);
                $cityDao->insertCity($city, $countryDao->retrieveId($country));
                $countryDao->insertCountry();
                $cityDao->insertCity($tocity, $countryDao->retrieveId($tocountry));
                $trip = new Trip('default',
                                $userId,
                                $cityDao->retrieveId($city),
                                $cityDao->retrieveId($tocity),
                                $fromdate,
                                $todate,
                                $_POST['transport'],
                                $description);
                $tripDao->insertTrip($trip);
            }
        }
        else{
            header('Location: index.php');
        }
    }
    else{
        header('Location: index.php?');
    }

?>
